Page({

  /**
   * 页面的初始数据
   */
  data: {
    positionFixed: "",//是否“最新、最热”显示在顶部
    newIsShow: true,//“最新”是否显示
    hotIsShow: false,//“最热”是否显示
    isFolded: "show",//是否显示【全文】二字：针对文字溢出
    voiceBoFangImage: 'minicard/home/19weibofanganniu.png',// 播放器的图片
    voiceCurrentTime: 0,//播放进度:当前进度
    voiceTotalTime: 401,//总时长
    voiceStartDate: "00:00", //日期格式的开始时间
    voiceTotalDate: "04:41"//日期格式的总时长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 播放或暂停动作
   */
  voiceBoFang: function (e) {
    var that = this;
    var voiceBoFangImage = that.data.voiceBoFangImage;
    var backgroundAudioManager = wx.getBackgroundAudioManager();//获取全局唯一的背景音频管理器
    if (voiceBoFangImage == "minicard/home/20bofanganniu.png") {
      // 切换至暂停状态
      wx.pauseBackgroundAudio();//暂停播放音乐
      backgroundAudioManager.pause();//暂停播放音乐
      that.setData({
        voiceBoFangImage: 'minicard/home/19weibofanganniu.png'
      });
    } else {
      // 切换至播放状态
      wx.stopBackgroundAudio();////暂停正在播放音乐
      backgroundAudioManager.startTime = that.data.voiceCurrentTime;//设置音乐开始时间
      backgroundAudioManager.title = '11';
      backgroundAudioManager.src = "http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46";
      backgroundAudioManager.play();
      //    背景音频自然播放结束事件
      backgroundAudioManager.onEnded(function () {
        that.setData({
          voiceCurrentTime: 0,
          voiceBoFangImage: 'minicard/home/19weibofanganniu.png'
        });
      })
      //背景音频播放进度更新事件
      backgroundAudioManager.onTimeUpdate(function (callback) {
        var position = parseInt(backgroundAudioManager.currentTime);
        var voiceStartDate = that.digitalFormatTime(position);
        that.setData({
          voiceCurrentTime: backgroundAudioManager.currentTime,
          voiceStartDate: voiceStartDate
        });
      })
      that.setData({
        voiceBoFangImage: 'minicard/home/20bofanganniu.png'
      });
    }
  },

  /**
   * 播放音乐（滚动条事件）
   */
  voiceSlider: function (e) {
    var that = this;
    var position = e.detail.value;
    var voiceStartDate = that.digitalFormatTime(position);
    var voiceBoFangImage = that.data.voiceBoFangImage;
    var backgroundAudioManager = wx.getBackgroundAudioManager();//获取全局唯一的背景音频管理器
    if (voiceBoFangImage == "minicard/home/20bofanganniu.png") {
      //正在播放状态
      wx.seekBackgroundAudio({
        position: Math.floor(position),
      })
    } else {
      // 未播放状态
      that.setData({
        voiceCurrentTime: position,
        voiceStartDate: voiceStartDate
      });
    }
  },

  /**
   * 数字转换为时间
   */
  digitalFormatTime: function (e) {
    var hourSize = 1 * 60 * 60;
    var minuteSize = 1 * 60;
    if (e > hourSize) {
      var hour = parseInt(e / hourSize);
      var minute = parseInt(e / minuteSize);
      var second = e % minuteSize;
      if (hour < 10) {
        hour = "0" + hour;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      return hour + ":" + minute + ":" + second;
    } else if (e > minuteSize) {
      var minute = parseInt(e / minuteSize);
      var second = e % minuteSize;
      if (minute < 10) {
        minute = "0" + minute;
      }
      return minute + ":" + second;
    } else {
      if (e < 10) {
        e = "0" + e;
      }
      return "00:" + e;
    }
  },

  /**
   * 跳转至视频播放页面
   */
  toVideo: function (e) {
    wx.navigateTo({
      url: '../../../home/video/video',
    })
  },

  /**
   * 展开或隐藏“选择个性标签”
   */
  xzLabelTap: function () {
    var that = this;
    var xzLabelDisplay = that.data.xzLabelDisplay;
    var newXzLabelDisplay = "block";
    if (xzLabelDisplay == "block") {
      newXzLabelDisplay = "none";
    }
    that.setData({
      xzLabelDisplay: newXzLabelDisplay
    });
  },

  /**
   * 全文展开或者收缩
   */
  qw_change: function (e) {
    this.setData({
      isFolded: !this.data.isFolded,
    })
  },

  /**
   * 最新或者最热切换
   */
  hotAndNewTap: function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    switch (type) {
      case "1":
        // 最新
        that.setData({
          hotIsShow: true,
          newIsShow: false
        });
        break;
      case "2":
        // 最热
        that.setData({
          hotIsShow: false,
          newIsShow: true
        });
        break;
    }
  },

  /**
   * 监听屏幕滚动 判断上下滚动
   */
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop);
    var query = wx.createSelectorQuery();
    query.select('.page_top').boundingClientRect(function (rect) {
      var wz = parseInt(rect.height + 14);
      if (scrollTop >= wz) {
        that.setData({
          positionFixed: "positionFixed"
        })
      } else {
        that.setData({
          positionFixed: ""
        })
      }
    }).exec();
  },

  /**
   * 返回活动
   */
  returnToNext: function(e){
    wx.navigateBack({
      delta: 1
    })
  }
})